﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using ProjectThuCung.Models;

namespace ProjectThuCung.Controllers.Admin
{
    public class AThuCungController : Controller
    {
        private ThuCungDB db = new ThuCungDB();

        //
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.TenSanPhamSortParm = String.IsNullOrEmpty(sortOrder) ? "TenSanPham desc" : "";
            ViewBag.LoaiSanPhamSortParm = sortOrder == "LoaiSanPham" ? "LoaiSanPham desc" : "LoaiSanPham";
            ViewBag.GiongSortParm = sortOrder == "Giong" ? "Giong desc" : "Giong";
            ViewBag.XuatXuSortParm = sortOrder == "XuatXu" ? "XuatXu desc" : "XuatXu";
            ViewBag.GioiTinhSortParm = sortOrder == "GioiTinh" ? "GioiTinh desc" : "GioiTinh";
            ViewBag.MauSortParm = sortOrder == "Mau" ? "Mau desc" : "Mau";
            ViewBag.ThangTuoiSortParm = sortOrder == "ThangTuoi" ? "ThangTuoi desc" : "ThangTuoi";
            ViewBag.TrongLuongSortParm = sortOrder == "TrongLuong" ? "TrongLuong desc" : "TrongLuong";
            ViewBag.GiaSortParm = sortOrder == "Gia" ? "Gia desc" : "Gia";
            ViewBag.SoLuongSortParm = sortOrder == "SoLuong" ? "SoLuong desc" : "SoLuong";
            
            
            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var sanPham = from s in db.SanPhams
                          where s.LoaiSanPham.SubLoaiSanPham == SubLoaiSP.ThuCung
                              select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                sanPham = sanPham.Where(s => s.TenSanPham.ToUpper().Contains(searchString.ToUpper()));
            }

            switch (sortOrder)
            {
                case "TenSanPham desc":
                    sanPham = sanPham.OrderByDescending(s => s.TenSanPham);
                    break;
                case "LoaiSanPham":
                    sanPham = sanPham.OrderBy(s => s.LoaiSanPham.TenLoaiSanPham);
                    break;
                case "LoaiSanPham desc":
                    sanPham = sanPham.OrderByDescending(s => s.LoaiSanPham.TenLoaiSanPham);
                    break;
                case "Giong":
                    sanPham = sanPham.OrderBy(s => s.ThongTinThem.Giong);
                    break;
                case "Giong desc":
                    sanPham = sanPham.OrderByDescending(s => s.ThongTinThem.Giong);
                    break;
                case "XuatXu":
                    sanPham = sanPham.OrderBy(s => s.XuatXu.TenXuatXu);
                    break;
                case "XuatXu desc":
                    sanPham = sanPham.OrderByDescending(s => s.XuatXu.TenXuatXu);
                    break;
                case "GioiTinh":
                    sanPham = sanPham.OrderBy(s => s.ThongTinThem.GioiTinh);
                    break;
                case "GioiTinh desc":
                    sanPham = sanPham.OrderByDescending(s => s.ThongTinThem.GioiTinh);
                    break;
                case "Mau":
                    sanPham = sanPham.OrderBy(s => s.ThongTinThem.Mau);
                    break;
                case "Mau desc":
                    sanPham = sanPham.OrderByDescending(s => s.ThongTinThem.Mau);
                    break;
                case "ThangTuoi":
                    sanPham = sanPham.OrderBy(s => s.ThongTinThem.ThangTuoi);
                    break;
                case "ThangTuoi desc":
                    sanPham = sanPham.OrderByDescending(s => s.ThongTinThem.ThangTuoi);
                    break;
                case "TrongLuong":
                    sanPham = sanPham.OrderBy(s => s.ThongTinThem.TrongLuong);
                    break;
                case "TrongLuong desc":
                    sanPham = sanPham.OrderByDescending(s => s.ThongTinThem.TrongLuong);
                    break;
                case "Gia":
                    sanPham = sanPham.OrderBy(s => s.Gia);
                    break;
                case "Gia desc":
                    sanPham = sanPham.OrderByDescending(s => s.Gia);
                    break;
                case "SoLuong":
                    sanPham = sanPham.OrderBy(s => s.SoLuong);
                    break;
                case "SoLuong desc":
                    sanPham = sanPham.OrderByDescending(s => s.SoLuong);
                    break;
                default:
                    sanPham = sanPham.OrderBy(s => s.TenSanPham);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View("Index", sanPham.ToPagedList(pageNumber, pageSize));
        }

        //public ActionResult Index()
        //{
        //    var sanphams = db.SanPhams.Include(s => s.LoaiSanPham).Include(s => s.XuatXu).Include(s => s.ThongTinThem);
        //    return View(sanphams.ToList());
        //}

        //
        // GET: /AThuCung/Details/5

        public ActionResult Details(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // GET: /AThuCung/Create

        public ActionResult Create()
        {
            List<SelectListItem> selectListXuatXu = new List<SelectListItem>();

            foreach (XuatXu xuatXu in db.XuatXus.ToList())
            {
                selectListXuatXu.Add(new SelectListItem() { Selected = false, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
            }

            ViewBag.LoaiSanPhamID = new SelectList(db.LoaiSanPhams.Where(s => s.SubLoaiSanPham == SubLoaiSP.ThuCung), "LoaiSanPhamID", "TenLoaiSanPham");
            ViewBag.XuatXuID = selectListXuatXu;

            return View();
        }

        //
        // POST: /AThuCung/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(SanPham sanpham)
        {
            sanpham.ThongTinThem = db.ThongTinThems.Add(sanpham.ThongTinThem);
            db.SaveChanges();
            sanpham.ThongTinThemID = sanpham.ThongTinThem.ThongTinThemID;

            if (sanpham.ThongTinThem.ThongTinThemID != 0)
            {
                db.SanPhams.Add(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            List<SelectListItem> selectListXuatXu = new List<SelectListItem>();

            foreach (XuatXu xuatXu in db.XuatXus.ToList())
            {
                selectListXuatXu.Add(new SelectListItem() { Selected = false, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
            }

            ViewBag.LoaiSanPhamID = new SelectList(db.LoaiSanPhams.Where(s => s.SubLoaiSanPham == SubLoaiSP.ThuCung), "LoaiSanPhamID", "TenLoaiSanPham", sanpham.LoaiSanPhamID);
            ViewBag.XuatXuID = selectListXuatXu;

            return View(sanpham);
        }

        //
        // GET: /AThuCung/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            
            
            if (sanpham == null)
            {
                return HttpNotFound();
            }

            sanpham.ThongTinThem = db.ThongTinThems.Find(sanpham.ThongTinThemID);

            if (sanpham.ThongTinThem == null)
            {
                return HttpNotFound();
            }

            List<SelectListItem> selectListXuatXu = new List<SelectListItem>();

            foreach (XuatXu xuatXu in db.XuatXus.ToList())
            {
                if (xuatXu.XuatXuID == sanpham.XuatXuID)
                {
                    selectListXuatXu.Add(new SelectListItem() { Selected = true, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
                }
                else
                {
                    selectListXuatXu.Add(new SelectListItem() { Selected = false, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
                }
            }

            ViewBag.LoaiSanPhamID = new SelectList(db.LoaiSanPhams, "LoaiSanPhamID", "TenLoaiSanPham", sanpham.LoaiSanPhamID);
            ViewBag.XuatXuID = selectListXuatXu;

            return View(sanpham);
        }

        //
        // POST: /AThuCung/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(SanPham sanpham)
        {
            ThongTinThem thongTinThem = sanpham.ThongTinThem;
            sanpham.ThongTinThem = null;

            if (ModelState.IsValid)
            {
                db.Entry(thongTinThem).State = EntityState.Modified;
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            List<SelectListItem> selectListXuatXu = new List<SelectListItem>();

            foreach (XuatXu xuatXu in db.XuatXus.ToList())
            {
                if (xuatXu.XuatXuID == sanpham.XuatXuID)
                {
                    selectListXuatXu.Add(new SelectListItem() { Selected = true, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
                }
                else
                {
                    selectListXuatXu.Add(new SelectListItem() { Selected = false, Text = "[" + xuatXu.TenXuatXu + "] " + xuatXu.DiaChi, Value = xuatXu.XuatXuID.ToString() });
                }
            }

            ViewBag.LoaiSanPhamID = new SelectList(db.LoaiSanPhams, "LoaiSanPhamID", "TenLoaiSanPham", sanpham.LoaiSanPhamID);
            ViewBag.XuatXuID = selectListXuatXu;

            return View(sanpham);
        }

        //
        // GET: /AThuCung/Delete/5

        public ActionResult Delete(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            
            int count = db.CTHoaDons.Count(h => h.SanPhamID == sanpham.SanPhamID);

            if (count == 0)
            {
                ViewBag.Message = "Bạn có muốn xóa?";
                ViewBag.CanDelete = true;
            }
            else
            {
                ViewBag.Message = "Không thể xóa thú cưng này vì nó đã thực hiện quá trình mua bán.";
                ViewBag.CanDelete = false;
            }

            return View(sanpham);
        }

        //
        // POST: /AThuCung/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            db.SanPhams.Remove(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}